<script setup lang='ts'>
  import { onMounted, onActivated, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, $router } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import xmImage from '@/components/xmImage/index.vue'
  import Pagination from "@/components/Pagination/index.vue";
  import ticketForm from './ticketForm.vue';
  const ticketFormRef = ref()
  const action = reactive({
    total:0,
    activityID:0,
    cost_value:0,
    gameTypeList:[] as any[],
    list:[] as any[],
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    custom_id:'' as any,
  })
  const handleNumSubmit = ()=>{
    ElMessageBox.prompt('数量', '下发比例', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      inputPattern:/^\+?[1-9][0-9]*$/,
      inputPlaceholder:'请填写下发比例',
      inputErrorMessage: '请填写正整数下发比例',
    })
      .then(async({ value }) => {
        const loading = Toastloading()
        await gamesApi.customTicketConfigEdit({cost_value:value})
        loading.close()
        getTicketConfig()
        ToastSuccess('设置成功')
      }).catch(() => { })
  }
  const handleForm = async(row:any = {})=>{
    const { custom_id=listQuery.custom_id } = row
    if(action.gameTypeList.length == 0) {
      await getGameType()
      if(action.gameTypeList.length == 0) {
        return ToastWarning('缺少游戏类型')
      }
    }
    ticketFormRef.value.openDialog({
      ...row,
      custom_id,
      gameTypeList:action.gameTypeList
    })
  }
  const handleDelete = (row:any = {})=>{
    const { custom_id=0, id=0,game_type_text='' } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm(`确定删除活动ID：${custom_id}-${game_type_text}游戏吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      gamesApi.customTicketGameDel({id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getGameType = async()=>{
    const res = await gamesApi.customTicketGameType()
    const { data } = res
    action.gameTypeList = data
  }
  const getTicketConfig = async()=>{
    const res = await gamesApi.customTicketConfig()
    const { cost_value } = res.data
    action.cost_value = cost_value
  }
  const handleSerach = async()=>{
    action.list = []
    action.total = 0
    listQuery.page = 1
    await getTicketConfig()
    await getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await gamesApi.customTicketGame(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data.map((res:any)=>{
      return {
        game_type_text:action.gameTypeList.find(item=>item.game_type == res.game_type)?.game_name || res.game_type,
        ...res
      }
    })
  }
  let primarySet = false
  const primaryGetList = async()=>{
    if(primarySet) return
    primarySet = true
    const activityID = $router().currentRoute.value.query?.activityID || ''
    if(listQuery.custom_id != activityID && activityID) {
      listQuery.custom_id = activityID
      const loading = Toastloading()
      await getTicketConfig()
      await getGameType()
      await getList()
      primarySet = false
      loading.close()
    }
  }
  onMounted(async()=>{
    primaryGetList()
  })
  onActivated(async()=>{
    primaryGetList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">活动ID：</div>
          <el-input type="text" v-model="listQuery.custom_id" placeholder="请输入活动ID" />
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" v-rule="'stencil_ticketAdd'" @click="handleForm()">新增游戏</el-button>
          <el-button type="primary" v-rule="'stencil_ticketCost'" @click="handleNumSubmit">编辑下发比例</el-button>
        </div>
      </div>
    </div>
    <div v-rule="'stencil_ticketCost'" class="labelBox TablelabelBox xm-mb10">
      <div class="labelItem">
        <div class="label">下发比例：</div>消耗{{ action.cost_value }}金豆/钻石获得1人气票
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="120" prop="id" label="ID" />
      <el-table-column width="120" prop="custom_id" label="活动ID" />
      <el-table-column width="220" prop="game_type_text" label="游戏类型" />
      <el-table-column width="280" prop="img_url" label="图片">
        <template #default="scoped">
          <xmImage :src="scoped.row.img_url" :list="[scoped.row.img_url]" />
        </template>
      </el-table-column>
      <el-table-column min-width="220" prop="jump_url" label="跳转地址" />
      <el-table-column width="180" fixed="right" label="操作">
        <template #default="scoped">
          <el-button type="primary" v-rule="'stencil_ticketEdit'" @click="handleForm(scoped.row)">编辑</el-button>
          <el-button type="danger" v-rule="'stencil_ticketDel'" @click="handleDelete(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <ticketForm ref="ticketFormRef" @update-list="getList" />
  </div>
  
</template>